package com.hl.taxinfo.dao;
// default package

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.hl.taxinfo.model.ZyClass;
import com.hl.taxinfo.model.ZyFirst;

/**
 	* A data access object (DAO) providing persistence and search support for ZyClass entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .ZyClass
  * @author MyEclipse Persistence Tools 
 */
@Transactional
public class ZyClassDAOImpl extends HibernateDaoSupport implements ZyClassDAO  {
		 private static final Log log = LogFactory.getLog(ZyClassDAOImpl.class);
		//property constants
	public static final String CLASS_PID = "classPid";
	public static final String CLASS_NAME = "className";
	public static final String CLASS_INFO = "classInfo";



	protected void initDao() {
		//do nothing
	}
    
    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#save(com.hl.taxinfo.model.ZyClass)
	 */
    public void save(ZyClass transientInstance) {
        log.debug("saving ZyClass instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	/* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#delete(com.hl.taxinfo.model.ZyClass)
	 */
	public void delete(ZyClass persistentInstance) {
        log.debug("deleting ZyClass instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	 public void update(ZyClass transientInstance) {
	        log.debug("saving ZyClass instance");
	        try {
	            getHibernateTemplate().update(transientInstance);
	            log.debug("update successful");
	        } catch (RuntimeException re) {
	            log.error("update failed", re);
	            throw re;
	        }
	    }
    
    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findById(java.lang.Integer)
	 */
    public ZyClass findById( java.lang.Integer id) {
        log.debug("getting ZyClass instance with id: " + id);
        try {
            ZyClass instance = (ZyClass) getHibernateTemplate()
                    .get("com.hl.taxinfo.model.ZyClass", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findByExample(com.hl.taxinfo.model.ZyClass)
	 */
    public List findByExample(ZyClass instance) {
        log.debug("finding ZyClass instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ZyClass instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ZyClass as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	/* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findByClassPid(java.lang.Object)
	 */
	public List findByClassPid(Object classPid
	) {try{
		return findByProperty(CLASS_PID, classPid);
	}catch(Exception e){
		System.out.println("List is empty");
		return null;
	}
	}
	
	/* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findByClassName(java.lang.Object)
	 */
	public List findByClassName(Object className
	) {
		return findByProperty(CLASS_NAME, className
		);
	}
	
	/* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findByClassInfo(java.lang.Object)
	 */
	public List findByClassInfo(Object classInfo
	) {
		return findByProperty(CLASS_INFO, classInfo
		);
	}
	

	/* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all ZyClass instances");
		try {
			String queryString = "from ZyClass";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#merge(com.hl.taxinfo.model.ZyClass)
	 */
    public ZyClass merge(ZyClass detachedInstance) {
        log.debug("merging ZyClass instance");
        try {
            ZyClass result = (ZyClass) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#attachDirty(com.hl.taxinfo.model.ZyClass)
	 */
    public void attachDirty(ZyClass instance) {
        log.debug("attaching dirty ZyClass instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see com.hl.taxinfo.dao.ZyClassDAO#attachClean(com.hl.taxinfo.model.ZyClass)
	 */
    public void attachClean(ZyClass instance) {
        log.debug("attaching clean ZyClass instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static ZyClassDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (ZyClassDAO) ctx.getBean("ZyClassDAO");
	}
}